package com.ruoyi.other.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.other.domain.CityFareConfig;
import com.ruoyi.other.mapper.CityConfigMapper;
import com.ruoyi.other.service.ICityFareConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * 城市路费配置Controller
 *
 * @author ruoyi
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/cityFareConfig/cityFareConfig")
public class CityFareConfigController extends BaseController {
    @Autowired
    private ICityFareConfigService cityFareConfigService;
    @Resource
    private CityConfigMapper cityConfigMapper;

    /**
     * 查询城市路费配置列表
     */
    @PreAuthorize("@ss.hasPermi('cityFareConfig:cityFareConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(CityFareConfig cityFareConfig) {
        if (!CheckEmptyUtil.isEmpty(cityFareConfig.getCityName())) {
            Long id = cityConfigMapper.selectIdByName(cityFareConfig.getCityName());
            if (!CheckEmptyUtil.isEmpty(id)) {
                cityFareConfig.setCityId(id);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<CityFareConfig> list = cityFareConfigService.selectCityFareConfigList(cityFareConfig);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (CityFareConfig fareConfig : list) {
                String cityName = cityConfigMapper.selectCityName(fareConfig.getCityId());
                fareConfig.setCityName(CheckEmptyUtil.isEmpty(cityName) ? "" : cityName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出城市路费配置列表
     */
    @PreAuthorize("@ss.hasPermi('cityFareConfig:cityFareConfig:export')")
    @Log(title = "城市路费配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CityFareConfig cityFareConfig) {
        List<CityFareConfig> list = cityFareConfigService.selectCityFareConfigList(cityFareConfig);
        ExcelUtil<CityFareConfig> util = new ExcelUtil<CityFareConfig>(CityFareConfig.class);
        util.exportExcel(response, list, "城市路费配置数据");
    }

    /**
     * 获取城市路费配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('cityFareConfig:cityFareConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cityFareConfigService.selectCityFareConfigById(id));
    }

    /**
     * 新增城市路费配置
     */
    @PreAuthorize("@ss.hasPermi('cityFareConfig:cityFareConfig:add')")
    @Log(title = "城市路费配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CityFareConfig cityFareConfig) {
        return toAjax(cityFareConfigService.insertCityFareConfig(cityFareConfig));
    }

    /**
     * 修改城市路费配置
     */
    @PreAuthorize("@ss.hasPermi('cityFareConfig:cityFareConfig:edit')")
    @Log(title = "城市路费配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CityFareConfig cityFareConfig) {
        return toAjax(cityFareConfigService.updateCityFareConfig(cityFareConfig));
    }

    /**
     * 删除城市路费配置
     */
    @PreAuthorize("@ss.hasPermi('cityFareConfig:cityFareConfig:remove')")
    @Log(title = "城市路费配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cityFareConfigService.deleteCityFareConfigByIds(ids));
    }


    @GetMapping("selectCityFareCity")
    public AjaxResult selectCityFareCity() {
        return success(cityFareConfigService.selectCityFareCity());
    }


}
